package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

//注册账户  提示错误 or  注册成功？的servlet
//ps.!!!这里webservlet中的name，和之前都不太一样,写NewAccountServlet会出现404
@WebServlet(name="NewAccount",urlPatterns = {"/newAccount"})
public class NewAccountServlet extends HttpServlet {
    private static final String MAIN_FORM = "/WEB-INF/jsp/catalog/main.jsp";
    private static final String NEW_ACCOUNT_FORM = "/WEB-INF/jsp/account/newAccount.jsp";

    private Account account;
    private Account account1;
    private AccountService accountService;
    private boolean isValid=true;

    public String msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //下面判断用户名是否可用
        String username=req.getParameter("username");
        //给js的响应是一个文本
        resp.setContentType("text/plain");//响应默认是同步的，这里要设置成异步的
        PrintWriter out=resp.getWriter();
        accountService=new AccountService();
        Account account=accountService.getAccount(username);
        if(account.getUsername()==null||account.getUsername().equals("")){
            //可用
            out.print("Not Exist");
        }else{
            //不可用
            out.print("Exist");
        }


        }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }


}
